package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.dto.PmsProductCategoryWithChildrenItem;
import cn.zcscloud.mall.mbg.mapper.PmsProductCategoryMapper;
import cn.zcscloud.mall.mbg.model.PmsProductCategory;
import cn.zcscloud.mall.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: PmsProductCategoryServiceImpl
 * Description:
 * date: 2022/4/2 10:58
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper,PmsProductCategory> implements PmsProductCategoryService {
    @Override
    public List<PmsProductCategoryWithChildrenItem> withChildren() throws InvocationTargetException, IllegalAccessException {

        List<PmsProductCategoryWithChildrenItem> data = new ArrayList<>();
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId,0);
        List<PmsProductCategory> list = this.list(queryWrapper);
        for (PmsProductCategory pmsProductCategory : list) {
            LambdaQueryWrapper<PmsProductCategory> qw = new LambdaQueryWrapper<>();
            PmsProductCategoryWithChildrenItem childrenItem = new PmsProductCategoryWithChildrenItem();
            qw.eq(PmsProductCategory::getParentId,pmsProductCategory.getId());
            List<PmsProductCategory> childrenList = this.list(qw);
            BeanUtils.copyProperties(childrenItem,pmsProductCategory);
            childrenItem.setChildren(childrenList);
            data.add(childrenItem);
        }
        return data;
    }
}
